home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 16 / sets / setsourc / setinter.c < prev    next >
C/C++ Source or Header  |  1989-03-09  |  2KB  |  72 lines

  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include "sets.h"
  4. /***************************************************************************/
  5.             int set_intersect(set *intersect, set *set1, set *set2)
  6. /***************************************************************************/
  7. /* This procedure computes the intersection (set-product) of two sets.  The
  8.    sets must have the same base_type in order for this to work.  The input
  9.    parameters are left unchanged.
  10. */
  11. {
  12. int i;
  13. defset(temp,UNIVERSAL,MAX_MEMBERS,0);
  14. set *temp2,*temp3;
  15.  
  16.     /* first check for the same base_types and tags */
  17.     if(!cmp_base_types(set1,set2) || !cmp_base_types(set1,intersect) ||
  18.        !cmp_set_tags(set1,set2)   || !cmp_set_tags(set1,intersect))
  19.         return FAILURE;
  20.  
  21.     /* The intersection of two sets is a set whose members are members only of
  22.        both the intersecting sets.  Determine the intersection and place it in
  23.        the output set called 'intersect.'
  24.     */
  25.  
  26.     /* next clear the output set */
  27.     set_clear(&temp);
  28.  
  29.     /* next check for empty sets on the input.  If so, return empty set. */
  30.     if(set1->nmembers == 0 || set2->nmembers == 0)
  31.         return SUCCESS;
  32.  
  33.     /* Since all sets include member locations starting at 0 and increasing
  34.        toward set_size - 1, the intersection of two sets can be found by
  35.        comparing the bits in the member records starting at bit 0 or, in this
  36.        implementation, starting with the first member word.  The matching
  37.        contents of member words can be determined simply by a bitwise-and
  38.        operation.
  39.     */
  40.     /* get the set with the smallest set_size */
  41.     temp2 = (set1->set_size > set2->set_size) ? set2 : set1;
  42.     if(temp2 == set1)
  43.         temp3 = set2;
  44.     else
  45.         temp3 = set1;
  46.  
  47.     /* temp2 points to the smallest set, temp3, the larger.  The set,
  48.        'intersect' must be at least the size of temp2.  Check this.
  49.     */
  50.     if(temp.set_size < temp2->set_size)
  51.         return FAILURE;
  52.  
  53.     /* Bitwise-and the respective member words of the intersecting sets
  54.        and assign the result to the output set.  Note that the smaller set
  55.        will have ALL bits at member locations higher than nmembers reset
  56.        to zero.
  57.     */
  58.     for(i=0;i<temp2->member_recs;i++)
  59.         temp.word[i] = temp2->word[i] & temp3->word[i];
  60.  
  61.     /* correct the member count in the set record */
  62.     temp.nmembers = set_member_count(&temp);
  63.  
  64.     /* assign temp to intersect */
  65.     set_assign(intersect,&temp);
  66.  
  67.     /* done. */
  68.     return SUCCESS;
  69.  
  70. }  /* end set_intersect */
  71.  
  72.